/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : backbone router 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-18 10:59:14
 * @filename : static/mnm/src/routers/router.js
 * @version : 
 * @description : 
 */
define(function(require, exports, module){
    var Backbone, router, prevObj = null,
        md5 = require('{component}md5/md5'), 
        cacheObj = {};
    Backbone = require('backbone');

    /**
     * #name/type/tab
     * name : 界面模块名称，非seajs模块名称
     * type : part 界面交给组件维护 / whole 界面交给系统维护 
     * tab  : type为part时有效，组件维护必要参数
     */
    router = Backbone.Router.extend({

        initialize: function(options) {
            var tthis = this;
            this.route(/.*/, "all", function(){tthis.routeUrl('index','whole')});
            this.route(":name/:type", "modules", function(name,type){tthis.routeUrl(name,type)});
            this.route(":name/:type/custom:val", "modulesC", function(name,type,val){tthis.routeUrl(name,type,val)});
            this.route(":name/:type/tab:num", "modulesT", function(name,type,num){tthis.routeUrl(name,type,num)});
            this.route(":name/:type/pay:key", "modulesP", function(name,type,key){tthis.routeUrl(name,type,'',key)});
        },

        routeUrl : function(name,type,val,key) {
            if (name != 'upgrade' && name != 'payment') {
                share.setStorage("cur_url", window.location.href);
            }
            if (key) {
                userInfo = share.userInfo();
                if (key == md5(userInfo.usr_id+userInfo.username+'paid')) {
                    share.editStorage('ms_user_info','isGuest',0);
                    this.delIdPage('userNav');
                }
            }
            var obj = {
                    name : name,
                    type : type,
                    num  : val,//历史问题
                    val  : val
                };
            this.dispatcher(obj); 
        },

        dispatcher : function(obj) {
            this.routesDoFuncBefore(obj);
            if (cacheObj[obj.name]) {
                //console.log('initialize',cacheObj[obj.name].initialize); 
                cacheObj[obj.name].render(obj); 
                seajs.moduleUI = obj.name;
            } else {
                share.pageLoad(true,$('body'));
                seajs.moduleUI = '';//
                require.async('{controller}'+obj.name,function(module){
                    if (module) {
                        //IOS,BUG
                        $('.page').css('display','none');
                        share.pageLoad(false,$('body'));
                        seajs.moduleUI = obj.name;
                        var s = new module();
                        s.render(obj); 
                        cacheObj[obj.name] = s; 
                    } else {
                        alert('router name is error!');
                        //window.location.href = '#index/whole'; 
                    }
                });
            }
        },

        routesDoFuncBefore : function(obj) {
            $('.page').css('display','none');
            // ios BUG 无法失去焦点
            if (prevObj) {
                //$('#'+prevObj.name).css('display','none');
                var input = $('#'+prevObj.name).find('input'),
                    select = $('#'+prevObj.name).find('select');
                    textarea = $('#'+prevObj.name).find('textarea');
                    input.blur();
                    select.blur();
                    textarea.blur();
            }

            if (prevObj && (prevObj.name == 'messageList' || prevObj.name == 'messageConversation' ||  prevObj.name == 'mailDetail')) {
                if (obj.name != 'messageList' && obj.name != 'messageConversation' &&  obj.name != 'mailDetail') {
                    share.disconnect(); 
                }
            }

            if ($('.page').length > 10) {
                var id = $('.page').eq(0).attr('id');
                this.delIdPage(id);
            }
            prevObj = obj;
        },

        delIdPage : function(id) {
            if (typeof(cacheObj[id]) !== 'undefined') {
                $(window).off('.'+id); 
                $(document).off('.'+id); 
                $('body').off('.'+id);
                cacheObj[id].undelegateEvents(); 
                delete cacheObj[id];
            }
            $('#'+id).remove();
        }
    });
    module.exports = router;
})

